import Vue from 'vue'
import Vuex from 'vuex'
import instance from '../httpconfig/http'
import router from '../config/router'
import {Message} from 'element-ui'
import axios from 'axios'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    CompetitionName:'',
    defaultActive:'',
    token:'',
    code:'',
    sid:'',
    user:{},
    file:{},
    currentresource:[],
    allCompetition:{},
    document:[]
  },
  mutations: {
    setDefaultActive(state,data){
      state.defaultActive = data
      sessionStorage.defaultActive = data
    },
    setCurrentResource(state,data){
      state.currentresource = data
      // console.log(data)
    },
    setToken(state,data){
      state.token = data.data
      sessionStorage.token = state.token
      state.code = data.code
      state.user = data
    },
    setUserInfo(state,data){
      state.user = data
      state.user.createTime = data.createTime.substring(0,10)
      // console.log(state.user)
    },
    delToken(state) {
      state.token = ''
      state.defaultActive=''
      sessionStorage.removeItem('defaultActive')
      sessionStorage.removeItem('token')
      },
    //获取全部比赛信息
    getNewCompetition(state,data){
      state.allCompetition = data
      // console.log(state.allCompetition)
    },
    //查询学生自己的所有项目
    getProject(state,data){
      state.allCompetition = data
      // console.log(state.allCompetition)
    },
    //设置头像
    setUserImg(state,data){
      state.user.imgUrl = data
      // console.log(state.user.imgUrl)
    },
    //设置参加比赛名字
    setName(state,data){
      state.CompetitionName = data
      // console.log(state.CompetitionName)
    },
    //创建项目ID
    setProject(state,file){
      state.file = file
      // console.log( state.file)
    },
    //保存查询项目文档
    getDocument(state,data){
      state.document = data
      // console.log(state.document)
    },
    //查询单一文件的sid
    getSid(state,data){
      state.sid = data
      console.log(state.sid)
    }
      
  },
  actions: {
    //获得用户信息
    getUserInfo({commit}){
      instance.post('/user/getCurrentUser')
      .then( response => {
        // console.log(response);
        commit('setUserInfo',response.data.data);
      })
      .catch(function (error) {
        console.log(error);
      });
    },
    //登录
    getLogin({ commit }, data){
      instance.post('/user/login',data) 
      .then( response => {
        // console.log(response)   
          commit('setToken', response.data);
          router.push('/user/competition/addCompetition')
      })
      .catch(function (error) {
        console.log(error);
      });
    },
    //获取导航菜单
    getCurrentResource({commit}){
      instance.post('/user/getCurrentResource') 
      .then( response => {   
        // console.log(response)
        commit('setCurrentResource', response.data.data);
        // this.$router.push('/main/index')
      })
      .catch(function (error) {
        console.log(error);
      });
    },
    //修改用户信息
    editUser({state}){
      console.log({cname:state.user.cname,email:state.user.email,phoneNum:state.user.phoneNum,sex:state.user.sex})
      instance.put('/user/editUser',{cname:state.user.cname,email:state.user.email,phoneNum:state.user.phoneNum,sex:state.user.sex})
        .then( response => {
          // console.log(response)
          if(response.data.code != 200){
            Message.warning('网络繁忙，请稍后重试！！！');
          }else{
            if(response.data.code == 200){
              Message.success('修改成功！！！');
            }
          }
        })
        .catch( response => {
          console.log(response)
        })
    },
    //上传用户头像
    editUserImgUrl({},imgUUid){
      // console.log(imgUUid)
      instance.put('/user/editUserImgUrl',imgUUid)
        .then( res => {
          // console.log(res)
        })
        .catch(res =>{
          console.log(res)
        })
    },
    //获取全部比赛信息
    getAllCompetition({commit},data){
      instance.get(`/competition/getNewCompetition/${data.page}/${data.size}`)
        .then( response => {
          commit('getNewCompetition',response.data)
          // console.log(response)
        }).catch(response => {
          console.log(response)
        })
    },
    //参加比赛
    addProject({state,dispatch,commit},data){
      console.log(data)
      instance.post('/project/addProject',data)
        .then( res => {
          // if (res.data.code == 200){
            console.log(res)
            console.log({projectSid:res.data.data.sid,cname:state.file.name,type:'项目申报书',uuid:state.file.response.data})
              dispatch('addDocument',{projectSid:res.data.data.sid,cname:state.file.name,type:'项目申报书',uuid:state.file.response.data})
                .then( () => {
                    commit('setDefaultActive','/user/project/getProject')
                    router.push('/user/project/getProject')          
                    dispatch('getProject',{page:'1',size:'15'}) 
                })
          // }else{
          //   Message.warning('网络繁忙，请稍后重试！！！');
          // }
        })
        .catch( error => {
          console.log(error)
        })
    },
    //查询学生自己的所有项目
    getProject({commit},data){
      instance.get(`/project/getProject/${data.page}/${data.size}`)
        .then( res =>{
          commit('getProject',res.data)
          // console.log(res.data)
        })
        .catch( res =>{
          console.log(res)
        })
    },
    //获得参加比赛的名称
    getBySidCompetition({commit},sid){
      instance.get(`/competition/getBySidCompetition/${sid}`)
        .then( res => {
            commit('setName',res.data.data.cname)
        })
    },
    //上传文档
    addDocument({},data){
      instance.post('/document/addDocument',data)
        .then( res => {
          console.log(res)
        })
        .catch( res => {
          // console.log(res)
        })
    },
    //重修上传
    editDocument({},data){
      instance.put('/document/editDocument',data)
        .then( res => {
          // console.log(res)
        })
        .catch( res => {
          // console.log(res)
        })
    },
    //查询项目文档
    getDocument({commit},projectSid){
      instance.get(`/document/getDocument/${projectSid}`)
        .then( res => {
          commit('getDocument',res.data.data)
          // console.log(res)
        })
        .catch( res => {
          // console.log(res)
      })
    },
    //查询某个文档id
    getTypeDocument({commit},data){
      instance.get(`/document/getTypeDocument/${data.projectSid}/${data.type}`)
        .then( res => {
          console.log(res)
          commit('getSid',res.data.data.sid)
        })
        .catch( res => {
          // console.log(res)
      })
    },
    //下载文档
    Download({},uuid){
      axios({
        method:'get',
        url:`http://210.41.45.4/jxfl/download/single_download?sid=${uuid}`,
        responseType:'arraybuffer',
      })
        .then( res => {
          console.log(res)
        })
        .catch( res => {
          // console.log(res)
        })
    }
  }
})


export default store